package com.base.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class TreeNode {
	
	public static final String icon_folder_normal = "folder.gif";
	
	public static final String icon_folder_open = "folder_open.gif";
	
	public static final String icon_folder_selected = "folder_selected.gif";
	
	public static final String icon_leaf = "leaf.gif";
	
	public static final String icon_plus = "plus.gif";
	
	public static final String icon_sub = "sub.gif";
	
	private String id = "";
	
	private String name = "";
	
	private String title = "";
	
	private String pid = "";
	
	private String next = "";
	
	private String pre = "";
	
	private int level = 0;
	
	private String folderImg = "";// 目录或文件夹图片
	
	private String folderOpenImg = "";// 目录或文件夹被选中图片
	
	private String folderSelectedImg = "";// 选中图片 
	
	private String leafImg = "";// 叶子节点目录
	
	private boolean isLeaf = true; // 是目录
	
	private String imgHomeDir = "img/satantree/"; // 图标主目录 
	
	private boolean isOpen = false;// 是否展开
	
	private boolean isSelected = false;// 是否选中
	
	// 默认为treestyle，优先使用nodestyle
	private TreeNodeStyleType style = null;
	
	private List<TreeNode> children = new ArrayList<TreeNode>();
	
	private TreeEventManager eventMgr = null;
	
	private Map<String, String> infos = new HashMap<String, String>();
	
	private TreeNodeRender render = null;
	
	private boolean showLeafIcon = false;// 是否显示叶子节点展开收缩图标 
	
	public TreeNode(String name) {
		this.id = TreeUtil.requestId();
		this.name = name;
		render = new TreeNodeRender(this);
	}
	
	public TreeNode(String id, String name) {
		this(name);
		this.id = id;
	}
	
	public TreeNode(String name, boolean isLeaf) {
		this(name);
		this.isLeaf = isLeaf;
	}
	
	public TreeNode(String id, String name, boolean isLeaf) {
		this(name, isLeaf);
		this.id = id;
		this.title = name;
	}
	
	public String view() {
		return this.render.doView().toString();
	}
	

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getNext() {
		return next;
	}

	public void setNext(String next) {
		this.next = next;
	}

	public String getPre() {
		return pre;
	}

	public void setPre(String pre) {
		this.pre = pre;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public TreeNode[] getChildren() {
		return this.children.toArray(new TreeNode[this.children.size()]);
	}

	public TreeNodeStyleType getStyle() {
		return style;
	}

	public void setStyle(TreeNodeStyleType style) {
		this.style = style;
	}

	public String getImgHomeDir() {
		return imgHomeDir;
	}

	public void setImgHomeDir(String imgHomeDir) {
		this.imgHomeDir = imgHomeDir;
	}

	public boolean isOpen() {
		return isOpen;
	}

	public void setOpen(boolean isOpen) {
		this.isOpen = isOpen;
	}

	public boolean isSelected() {
		return isSelected;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}

	public boolean isLeaf() {
		return isLeaf;
	}

	public void setLeaf(boolean isLeaf) {
		this.isLeaf = isLeaf;
	}

	protected TreeEventManager getEventMgr() {
		return eventMgr;
	}

	protected void setEventMgr(TreeEventManager eventMgr) {
		this.eventMgr = eventMgr;
	}
	
	/**
	 * 添加子节点
	 * @param node
	 */
	public void addChild(TreeNode node) {
		node.pid = this.id;
		node.level = this.level + 1;
		
		if(this.children.size() > 0) {
			TreeNode lastNode = this.children.get(children.size() - 1);
			lastNode.next = node.id;
			node.pre = lastNode.id;
		}
		
		this.children.add(node);
	}
	
	/**
	 * 通过id获取子节点
	 * @param childId
	 * @return
	 */
	public TreeNode getChild(String childId) {
		TreeNode child = null;
		if(childId == null) return child;
		
		for(TreeNode node : this.children) {
			if(childId.equals(node.getId())) {
				child = node;
				break;
			}
		}
		return child;
	}
	
	/**
	 * 添加节点信息
	 * @param name
	 * @param value
	 */
	public void addInfo(String name, String value) {
		this.infos.put(name, value);
	}
	
	/**
	 * 获取节点信息
	 * @param name
	 * @return
	 */
	public String getInfo(String name) {
		return this.infos.get(name);
	}

	protected Map<String, String> getInfos() {
		return this.infos;
	}

	public TreeNodeRender getRender() {
		return render;
	}
	
	/**
	 * 节点转换为参数
	 * @return
	 */
	public StringBuffer toArgs() {
		StringBuffer args = new StringBuffer();
		args.append("id").append(TreeUtil.SEPARATOR_EQUAL).append(getId())
			.append(TreeUtil.SEPARATOR)
			.append("name").append(TreeUtil.SEPARATOR_EQUAL).append(getName())
			.append(TreeUtil.SEPARATOR)
			.append("title").append(TreeUtil.SEPARATOR_EQUAL).append(getTitle())
			.append(TreeUtil.SEPARATOR)
			.append("pid").append(TreeUtil.SEPARATOR_EQUAL).append(getPid())
			.append(TreeUtil.SEPARATOR)
			.append("next").append(TreeUtil.SEPARATOR_EQUAL).append(getNext())
			.append(TreeUtil.SEPARATOR)
			.append("pre").append(TreeUtil.SEPARATOR_EQUAL).append(getPre())
			.append(TreeUtil.SEPARATOR)
			.append("level").append(TreeUtil.SEPARATOR_EQUAL).append(getLevel())
			.append(TreeUtil.SEPARATOR)
			.append("isLeaf").append(TreeUtil.SEPARATOR_EQUAL).append(isLeaf());
		
		// 节点其他信息
		for(Entry<String, String> entry : getInfos().entrySet()) {
			args.append(TreeUtil.SEPARATOR);
			args.append(entry.getKey()).append(TreeUtil.SEPARATOR_EQUAL).append(entry.getValue());
		}
		
		return args;
	}

	public String getFolderImg() {
		return folderImg;
	}

	public void setFolderImg(String folderImg) {
		this.folderImg = folderImg;
	}

	public String getLeafImg() {
		return leafImg;
	}

	public void setLeafImg(String leafImg) {
		this.leafImg = leafImg;
	}

	public String getFolderOpenImg() {
		return folderOpenImg;
	}

	public void setFolderOpenImg(String folderOpenImg) {
		this.folderOpenImg = folderOpenImg;
	}

	public String getFolderSelectedImg() {
		return folderSelectedImg;
	}

	public void setFolderSelectedImg(String folderSelectedImg) {
		this.folderSelectedImg = folderSelectedImg;
	}

	public boolean isShowLeafIcon() {
		return showLeafIcon;
	}

	public void setShowLeafIcon(boolean showLeafIcon) {
		this.showLeafIcon = showLeafIcon;
	}
	
	
}
